'''
Created on January 11, 2010

This module contains all Package Management related classes and functions
'''

import subprocess
from os import popen

class PackageManagement(object) :
    '''
    Handles Package Management tasks, only RPM packages supported right now.
    '''
    
    
    def install(self, name = None, path = None):
        """
        in this function we can install packages with one of two ways:
            first to set the path variable to the path to the rpm we want to install
            second set the name variable to the name of the package we want to yum it
        """
        if (path is not None) & (name is None):
            if subprocess.call("rpm -ivh " + path + " > /dev/null 2>&1", shell=True):
                return False
            
            else:
                return True
        
        elif (path is None) & (name is not None):
            output = popen("yum -y install " + name)
            outputData = output.read()
            output.close()
            
            if outputData.strip().endswith('Nothing to do') :
                return False
            
            else:
                return True
        
        else :
            return False
            
            
    def remove(self, name):
        """
        remove package passed package name
        """
        pFile = popen("yum -y erase " + name)
        output = pFile.read()
        pFile.close()
        
        if output.strip().endswith('No Packages marked for removal') :
            return False
        
        else:
            return True
            
            
                
    
    def search(self, name):
        """
        search for the passed package name
        """
        
        output = popen("rpm -qa " + name)
        
        searchResult = []
        
        for line in output :
            searchResult.append(line.strip())
        
        output.close()
        return searchResult


    def listAll(self):
        """
        Return a list of all installed packages in the system.
        """
        
        packages = popen("rpm -qa")
        packageList = packages.read()
        
        packageList = packageList.strip().split("\n")
        return packageList
    
    
    def packageInfo(self,name):
        """
        get package info
        """
        
        packageInfo = popen("rpm -qi " + name)
        info = packageInfo.read()
        packageInfo.close()
        
        return info
        
def main():
    # THE FOLLOWING CODE IS JUST FOR TESTING
    # CODER DISCRITION IS ADVISED :D
    
    x = PackageManagement()
    
    if x.install(path="/media/media/WORK/programs/LinuxApps/RealPlayer11GOLD.rpm") :
        print "--#-Package installed successfully-#--"
    
    else :
        print "--#-Failed-#--"
        
    if x.install(name="vym"):
        print "--#-Package installed successfully-#--"
    
    else :
        print "--#-Failed-#--"
        
    if x.remove("gnote") :
        print "--#-Package Removed Successfully-#--"
    
    else :
        print "--#-Failed-#--"
    
    if x.search("vym"):
        print "--#-Package Found-#--"
    
    else:
        print "--#-Failed-#--"
    
    print x.listAll()
    
    print x.packageInfo('gnupg')
    
    x = PackageManagement()
    print x.search('gnupg')
    print x.search('gnome')

if __name__ == "__main__" :
    # Call the main function here to test this module
    #main()
    pass
